package com.ruoyi.framework.config;

import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializerBase;

import java.math.BigDecimal;

/**
 * Jackson BigDecimal转字符串序列化器
 *
 * @author wangzhaoqiang
 * @version 1.0
 * @date 2022/8/31 14:52
 */
@JacksonStdImpl
@SuppressWarnings("serial")
public class JacksonBigDecimalToStringSerializer
        extends ToStringSerializerBase
{
    /**
     * Singleton instance to use.
     */
    public final static JacksonBigDecimalToStringSerializer instance = new JacksonBigDecimalToStringSerializer();

    /**
     * <p>
     * Note: usually you should NOT create new instances, but instead use
     * {@link #instance} which is stateless and fully thread-safe. However,
     * there are cases where constructor is needed; for example,
     * when using explicit serializer annotations like
     * {@link com.fasterxml.jackson.databind.annotation.JsonSerialize#using}.
     */
    public JacksonBigDecimalToStringSerializer()
    {
        super(Object.class);
    }

    /**
     * Sometimes it may actually make sense to retain actual handled type.
     *
     * @since 2.5
     */
    public JacksonBigDecimalToStringSerializer(Class<?> handledType)
    {
        super(handledType);
    }

    @Override
    public final String valueToString(Object value)
    {
        if (value.getClass().equals(BigDecimal.class))
        {
            return ((BigDecimal) value).stripTrailingZeros().toPlainString();
        }
        else
        {
            return value.toString();
        }
    }
}
